package com.wei.tang.ui.course;

import com.wei.tang.AppConest;
import com.wei.tang.AppContext;
import com.wei.tang.R;
import com.wei.tang.base.BaseActivity;
import com.wei.tang.bean.ClassifyBean;
import com.wei.tang.bean.CourseBean;
import com.wei.tang.db.ArticleDao;
import com.wei.tang.db.ClassifyDao;
import com.wei.tang.db.CourseDao;
import com.wei.tang.util.Tlog;
import com.wei.tang.util.Util;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 用户注册界面
 *
 */
public class StudentWorkActivity extends BaseActivity {

	private StudentWorkActivity mySelf = this;

	@InjectView(R.id.et_classname)
	EditText et_classname;

	@InjectView(R.id.et_classdesc)
	EditText et_classdesc;

	@InjectView(R.id.tv_course_file_path)
	TextView tv_course_file_path;

	private String mclassname = "";
	private String mclassdesc = "";
	private String mclasspath = "";

	@Override
	public void initView() {
	}

	@Override
	public int getLayoutId() {
		return R.layout.course_student_work_activity;
	}

	@Override
	public void initData() {
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.teacher_work_activity;
	}

	@Override
	@OnClick({ R.id.btn_publish, R.id.rl_course_file_path })
	public void onClick(View v) {
		Intent intent = null;
		int id = v.getId();
		switch (id) {
		case R.id.btn_publish:
			handleLogin();
			break;
		case R.id.rl_course_file_path:
			showFileChooser();
			break;
		}
	}

	private void showFileChooser() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("*/*");
		intent.addCategory(Intent.CATEGORY_OPENABLE);

		try {
			startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 1);
		} catch (android.content.ActivityNotFoundException ex) {
			AppContext.showToast("Please install a File Manager.");
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 1:
			if (resultCode == RESULT_OK) {
				// Get the Uri of the selected file
				Uri uri = data.getData();
				String path = Util.getPath(this, uri);
				tv_course_file_path.setText(path);
			}
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void handleLogin() {

		if (prepareForLogin()) {
			return;
		}

		mclassname = et_classname.getText().toString();
		mclassdesc = et_classdesc.getText().toString();
		mclasspath = tv_course_file_path.getText().toString();
		showWaitDialog(R.string.loading);
		LoadClassTask loadClassTask = new LoadClassTask();
		loadClassTask.execute(0);
	}

	class LoadClassTask extends AsyncTask<Integer, Integer, Integer> {
		String msg = "";
		int code = AppConest.WS_SUCCESS;

		@Override
		protected Integer doInBackground(Integer... params) {
			ClassifyDao classifyDao = new ClassifyDao(mySelf);

			boolean result = classifyDao.insertUser(new ClassifyBean(AppContext.USERBEAN.getUser_location(), mclassname,
					mclassdesc, AppContext.USERBEAN.getUser_name(), mclasspath));
			if (result) {
				msg = "提交成功";
				code = AppConest.WS_SUCCESS;
			} else {
				msg = "提交失败";
				code = AppConest.WS_FALL;
			}
			return code;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			hideWaitDialog();

			if (code == AppConest.WS_FALL) {
				AppContext.showToast(msg);
			} else {
				AppContext.showToast(msg);
				mySelf.finish();
			}
		}
	}

	/**
	 * 对输入输入的数据进行检查
	 */
	private boolean prepareForLogin() {
		if (et_classname.length() == 0) {
			et_classname.setError("请输入课程名");
			et_classname.requestFocus();
			return true;
		}

		if (et_classdesc.length() == 0) {
			et_classdesc.setError("请输入课程描述信息");
			et_classdesc.requestFocus();
			return true;
		}

		if (tv_course_file_path.length() == 0) {
			tv_course_file_path.setError("请选择课程文件");
			tv_course_file_path.requestFocus();
			return true;
		}

		return false;
	}
}
